#!/usr/bin/env bash

#####################################################################
# File Name: install_tools.sh
# Version: V1.0
# Author: Brian Hsiung
# Created Time : 2025-02-21 16:48:23
# Description: 安装 gradle nodejs git
####################################################################

# set -ex
declare -r GRADLE_VERSION="8.7"
declare -r GRADLE_BASE_HOME="/usr/local/gradle"
declare -r NODEJS_BASE_HOME="/usr/local/nodejs"
OSS_BASE_URL="https://brianhsiung.oss-cn-hangzhou.aliyuncs.com"
DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$DIR" || exit

### 日志函数
log_info()    { echo -e "\033[34m[INFO] $*\033[0m"; }
log_success() { echo -e "\033[32m[SUCCESS] $*\033[0m"; }
log_warn()    { echo -e "\033[33m[WARNING] $*\033[0m"; }
log_error()   { echo -e "\033[31m[ERROR] $*\033[0m"; exit 1; }

### 检查依赖
check_dependency() {
    local -a required_cmds=("unzip" "wget" "tar") 
    local missing=()

    for cmd in "${required_cmds[@]}"; do
        if ! command -v "$cmd" &>/dev/null; then
            missing+=("$cmd")
        fi
    done

    ((${#missing[@]})) && log_error "缺失依赖命令: ${missing[*]}"
}

#----------------------------------------------------- Install Gradle -----------------------------------------------------#
install_gradle() {
    
    log_info "安装gradle $GRADLE_VERSION"

    if [ "$access_mode" = 'external' ] && [ ! -f "gradle-$GRADLE_VERSION-bin.zip" ]; then
        log_info "通过 $OSS_BASE_URL/deploy/gradle/gradle-$GRADLE_VERSION-bin.zip 下载gradle"
        wget -q "$OSS_BASE_URL/deploy/gradle/gradle-$GRADLE_VERSION-bin.zip" || log_error "下载gradle失败"
    fi

    [ -f "gradle-$GRADLE_VERSION-bin.zip" ] || log_error "本地无gradle-$GRADLE_VERSION-bin.zip，请检查"

    if [ ! -d "$GRADLE_BASE_HOME" ]; then
        mkdir -p $GRADLE_BASE_HOME
        unzip -q gradle-$GRADLE_VERSION-bin.zip -d $GRADLE_BASE_HOME
        chmod 755 -R $GRADLE_BASE_HOME
        # chmod 755 -R $GRADLE_BASE_HOME/gradle-$GRADLE_VERSION/bin
        # find $GRADLE_BASE_HOME/gradle-$GRADLE_VERSION/bin -type f -exec chmod 755 {} \;
    elif [ ! -f "$GRADLE_BASE_HOME/gradle-$GRADLE_VERSION/bin/gradle" ]; then
        log_error "未找到gradle命令，请检查gradle安装目录"
    fi

    log_success "gradle安装成功"

}

#----------------------------------------------------- Install NodeJS-----------------------------------------------------#
install_nodejs_version() {

    local version=$1
    log_info "安装nodejs $version"
    # cpu architecture
    local arch
    arch="$(uname -m)"

    case "$arch" in
        aarch64|arm64)
            arch_type=arm64 ;;
        amd64|x86_64)
            arch_type=x64;;
        *)
            log_error "不支持的架构: $arch" ;;
    esac

    local nodejs_pkg="node-$version-linux-$arch_type.tar.xz"

    if [ "$access_mode" = 'external' ] && [ ! -f "$nodejs_pkg" ]; then
        log_info "通过 $OSS_BASE_URL/deploy/nodejs/$nodejs_pkg 下载安装包"
        wget -q "$OSS_BASE_URL/deploy/nodejs/$nodejs_pkg" || log_error "下载 $nodejs_pkg 失败"
        log_success "下载 $nodejs_pkg 成功"   
    fi  

    [ -f "$nodejs_pkg" ] || log_error "本地无 $nodejs_pkg，请检查"

    if [ ! -d "$NODEJS_BASE_HOME/node-$version" ]; then
        mkdir -p "$NODEJS_BASE_HOME/node-$version"
        tar -xf "$nodejs_pkg" -C "$NODEJS_BASE_HOME/node-$version" --strip-components=1 --no-same-owner
        #find "$NODEJS_BASE_HOME/node-$version/bin" -type f -exec chmod 755 {} \;
        chmod 755 -R $NODEJS_BASE_HOME
    fi

    [ -f "$NODEJS_BASE_HOME/node-$version/bin/node" ] || log_error "未找到$NODEJS_BASE_HOME/node-$version/bin/node，请检查"

    log_success "安装nodejs $version成功"
    
}

install_nodejs() {

    install_nodejs_version "v12.22.12"
    install_nodejs_version "v14.21.3"
    install_nodejs_version "v16.20.2"
    # 由于Glibc版本问题，CentOS7上无法使用版本
    install_nodejs_version "v18.20.7"
    install_nodejs_version "v20.18.3"
    install_nodejs_version "v22.14.0"

}

#----------------------------------------------------- Install Git-----------------------------------------------------#
install_git_external() {
    
    log_info "在线安装git"
    dnf install -y git || yum install -y git || log_error "安装git失败"

}

install_git_internal() {
    
    # get cpu architecture
    log_info "离线安装git"

    # wget https://brianhsiung.oss-cn-hangzhou.aliyuncs.com/deploy/git/git.tar.gz

    [ -f "git.tar.gz" ] || log_error "本地无git.tar.gz，请检查"

    if [ ! -d "/usr/local/git" ]; then
        mkdir -p "/usr/local/git"
    fi

    tar -zxf "git.tar.gz" -C "/usr/local/git" --strip-components=1 --no-same-owner
    #find "/usr/local/git/bin" -type f -exec chmod 755 {} \;
    chmod 755 -R /usr/local/git

    [ -f "/usr/local/git/bin/git" ] || log_error "未找到/usr/local/git/bin/git，请检查"

    for file in /usr/local/git/bin/*; do ln -sfn "$file" /usr/local/bin/; done

    log_success "离线安装git成功, $(git --version)"
        
}

install_git() {
    # Install Git
    if ! command -v git > /dev/null 2>&1; then
        if [ "$access_mode" = 'external' ]; then
            install_git_external
        else
            install_git_internal
        fi        
    fi
}

main() {

    [ "$(id -u)" -ne 0 ] && log_error "请使用root用户执行脚本"

    if curl --output /dev/null --silent --head --fail --connect-timeout 5 --max-time 10 "$OSS_BASE_URL"/health; then
        log_info "使用在线安装模式"
        access_mode='external'
    fi

    # 检查依赖
    check_dependency

    # 安装Gradle
    install_gradle

    # 安装NodeJS
    install_nodejs

    #  安装Git
    install_git
}

main 